//
//  BankPickView.m
//  XKJY
//
//  Created by xiaokang100 on 2017/1/21.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "BankPickView.h"

@interface BankPickView () <UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) NSMutableArray *bankNameArrayM;

@property (nonatomic, strong) UIButton      *leftBtn;
@property (nonatomic, strong) UIButton      *rightBtn;
@property (nonatomic, strong) UILabel       *resultLabel;

@property (nonatomic, strong) UIPickerView  *pickerView;

@property (strong, nonatomic) UIView         *darkView;
@property (strong, nonatomic) UIView         *backView;

@property (strong, nonatomic) UIBezierPath   *bezierPath;
@property (strong, nonatomic) CAShapeLayer   *shapeLayer;

@end
@implementation BankPickView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight + 300);
        self.bankNameArrayM = [NSMutableArray arrayWithObject:@"请选择银行"];
        [self initData];
        
        [self initGesture];
    }
    return self;
}


- (void)show {
    
    [self initView];
}

- (void)initView {
    
    [self showInView:[[UIApplication sharedApplication].windows lastObject]];
    
    [self addSubview:self.darkView];
    [self addSubview:self.backView];
    [self.backView addSubview:self.leftBtn];
    [self.backView addSubview:self.rightBtn];
    [self.backView addSubview:self.resultLabel];
    [self.backView addSubview:self.pickerView];
    
    [self bezierPath];
    [self shapeLayer];
    
    _resultLabel.text = @"请选择银行";
    _resultLabel.textColor = BlackColor;
}

- (void)initData {
    [self addSubview:self.darkView];
    [self.darkView addSubview:self.backView];
    [self.backView addSubview:self.leftBtn];
    [self.backView addSubview:self.rightBtn];
    [self.backView addSubview:self.pickerView];
    [self.backView addSubview:self.resultLabel];
    
    //路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bankcard"ofType:@"plist"];
    //读取
    NSArray *cardArray = [NSArray arrayWithContentsOfFile:path];
    for (NSDictionary *dict in cardArray){
        [self.bankNameArrayM addObject:dict[@"bank_card_name"]];
    }
    
}

- (void)initGesture {
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [self addGestureRecognizer:tap];
}

- (void)showInView:(UIView *)view {
    
    __weak typeof(self) weakSelf = self;
    self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    weakSelf.alpha = 1;
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.backView.frame = CGRectMake(0, ScreenHeight-300, ScreenWidth, 300);
    }];
    
//    [UIView animateWithDuration:0.25 animations:^{
//        
//        CGPoint point = self.center;
//        point.y      -= 300;
//        self.center   = point;
//        
//    } completion:^(BOOL finished) {
//    }];
//    
//    [view addSubview:self];
}

- (void)dismiss {
    __weak typeof(self) weakSelf = self;
    
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.backView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 300);
    } completion:^(BOOL finished) {
        weakSelf.alpha = 0;
        [weakSelf removeFromSuperview];
    }];
    
//    [UIView animateWithDuration:0.25 animations:^{
//        
//        CGPoint point = self.center;
//        point.y      += 300;
//        self.center   = point;
//        
//    } completion:^(BOOL finished) {
//        
//        [self removeFromSuperview];
//    }];
}




- (void)leftBrnClick:(UIButton *)sender {
    
}

- (void)rightBtnClick:(UIButton *)sender {
    
}





- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.bankNameArrayM.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return self.bankNameArrayM[row];
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    self.resultLabel.text = self.bankNameArrayM[row];
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel* pickerLabel            = (UILabel*)view;
    
    if (!pickerLabel){
        pickerLabel                 = [[UILabel alloc] init];
        pickerLabel.textAlignment   = NSTextAlignmentCenter;
        pickerLabel.backgroundColor = [UIColor clearColor];
        pickerLabel.font            = [UIFont systemFontOfSize:17.0*Scale_W];
    }
    
    pickerLabel.text                = [self pickerView:pickerView titleForRow:row forComponent:component];
    
    return pickerLabel;
}


- (UIView *)darkView {
    if (!_darkView) {
        _darkView                 = [[UIView alloc]init];
        _darkView.frame           = self.frame;
        _darkView.backgroundColor = [UIColor blackColor];
        _darkView.alpha           = 0.3;
    }
    return _darkView;
}

- (UIView *)backView {
    if (!_backView) {
        _backView = [[UIView alloc]init];
        _backView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 300);
        _backView.backgroundColor = [UIColor whiteColor];
    }
    return _backView;
}

- (UIBezierPath *)bezierPath {
    if (!_bezierPath) {
        _bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.backView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(2, 2)];
    }
    return _bezierPath;
}

- (CAShapeLayer *)shapeLayer {
    if (!_shapeLayer) {
        _shapeLayer             = [[CAShapeLayer alloc] init];
        _shapeLayer.frame       = _backView.bounds;
        _shapeLayer.path        = _bezierPath.CGPath;
        _backView.layer.mask    = _shapeLayer;
    }
    return _shapeLayer;
}

- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView                 = [[UIPickerView alloc]init];
        _pickerView.frame           = CGRectMake(0, 50, ScreenWidth, 250);
        _pickerView.delegate        = self;
        _pickerView.dataSource      = self;
        _pickerView.backgroundColor = MainBackColor;
    }
    return _pickerView;
}

- (UIButton *)leftBtn {
    if (!_leftBtn) {
        _leftBtn       = [UIButton buttonWithType:UIButtonTypeSystem];
        _leftBtn.frame = CGRectMake(0, 0, 70, 50);
        _leftBtn.titleLabel.font = [UIFont systemFontOfSize:17*Scale_W];
        [_leftBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_leftBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBtn;
}

- (UIButton *)rightBtn {
    if (!_rightBtn) {
        _rightBtn       = [UIButton buttonWithType:UIButtonTypeSystem];
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:17*Scale_W];
        _rightBtn.frame = CGRectMake(ScreenWidth - 70, 0, 70, 50);
        [_rightBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}

- (UILabel *)resultLabel {
    if (!_resultLabel) {
        _resultLabel = [[UILabel alloc]init];
        _resultLabel.frame = CGRectMake(70, 0, ScreenWidth - 140, 50);
        _resultLabel.textAlignment = NSTextAlignmentCenter;
        _resultLabel.font = [UIFont systemFontOfSize:17.0*Scale_W];
    }
    return _resultLabel;
}

- (void)rightBtnAction:(UIButton *)button {
    
    if (self.confimBtnActionBlcok) {
        self.confimBtnActionBlcok(self.resultLabel.text);
    }
    
    [self dismiss];
}



@end
